/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.service.impl;

import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.edu.hzjt.dao.SemesterSummaryMapper;
import cn.qidisoft.edu.hzjt.model.SemesterSummary;
import cn.qidisoft.edu.hzjt.service.ISemesterSummaryService;
import cn.qidisoft.edu.hzjt.utils.LogType;

/**
 * 
 * @author hxq
 * @version $Id: SemesterSummaryService.java, v 0.1 2018年10月29日 下午1:24:45 hxq Exp $
 */
@Service("semesterSummaryService")
public class SemesterSummaryServiceImpl implements ISemesterSummaryService {
  @Autowired
  private SemesterSummaryMapper semesterSummaryMapper;
  protected static final LoggerAdapter LOGGER =
      LoggerAdapterFactory.getLogger(LogType.HZJT_LOGGER.val);

  /**
   * @see cn.qidisoft.edu.hzjt.service.ISemesterSummaryService#querySemesterSummaryPageList(cn.qidisoft.edu.hzjt.model.SemesterSummary,
   *      cn.qidisoft.core.common.pagination.Page)
   */
  @Override
  public PaginationBean<SemesterSummary> querySemesterSummaryPageList(
      SemesterSummary semesterSummary, Page page) {
    try {
      int c = semesterSummaryMapper.querySemesterSummaryPageCount(semesterSummary);
      int totalRecords = 0;
      totalRecords = (int) c;
      PaginationBean<SemesterSummary> pageHolder =
          new PaginationBean<SemesterSummary>(page, totalRecords);
      List<SemesterSummary> resultList =
          semesterSummaryMapper.querySemesterSummaryPageList(semesterSummary,
              new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                  pageHolder.getPageSize()));
      pageHolder.setPageList(resultList);
      return pageHolder;
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("querySemesterSummaryPageList [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(), ToStringBuilder.reflectionToString(
              semesterSummary + "===" + page, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.ISemesterSummaryService#addSemesterSummary(cn.qidisoft.edu.hzjt.model.SemesterSummary)
   */
  @Override
  @Transactional
  public int addSemesterSummary(SemesterSummary semesterSummary) {
    try {
      semesterSummaryMapper.insert(semesterSummary);
      return semesterSummary.getZjid();
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("addSemesterSummary [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(semesterSummary, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }

  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.ISemesterSummaryService#queryGuidBySemesterSummary(cn.qidisoft.edu.hzjt.model.SemesterSummary)
   */
  @Override
  public SemesterSummary queryGuidBySemesterSummary(SemesterSummary semesterSummary) {
    try {
      return semesterSummaryMapper.queryGuidBySemesterSummary(semesterSummary);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("queryGuidBySemesterSummary [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(semesterSummary, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.ISemesterSummaryService#queryByPK(java.lang.Integer)
   */
  @Override
  public SemesterSummary queryByPK(Integer id) {
    try {
      return semesterSummaryMapper.queryByPK(id);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("queryByPK [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(id, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.ISemesterSummaryService#modiftySemesterSummary(cn.qidisoft.edu.hzjt.model.SemesterSummary)
   */
  @Override
  @Transactional
  public int modiftySemesterSummary(SemesterSummary semesterSummary) {
    try {
      return semesterSummaryMapper.updateByPK(semesterSummary);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("modiftySemesterSummary [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(semesterSummary, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.ISemesterSummaryService#deleteSemesterSummary(cn.qidisoft.edu.hzjt.model.SemesterSummary)
   */
  @Override
  @Transactional
  public int deleteSemesterSummary(String[] ids) {
    try {
      int count = 0;
      for (String id : ids) {
        int num = semesterSummaryMapper.deleteByPK(Integer.parseInt(id));
        if (num >= 1) {
          count++;
        }
      }
      return count;
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("deleteSemesterSummary [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(ids, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.ISemesterSummaryService#querySemesterSummaryPageManagerList(cn.qidisoft.edu.hzjt.model.SemesterSummary,
   *      cn.qidisoft.core.common.pagination.Page)
   */
  @Override
  public PaginationBean<SemesterSummary> querySemesterSummaryPageManagerList(
      SemesterSummary semesterSummary, Page page) {
    try {
      int c = semesterSummaryMapper.querySemesterSummaryPageManagerCount(semesterSummary);
      int totalRecords = 0;
      totalRecords = (int) c;
      PaginationBean<SemesterSummary> pageHolder =
          new PaginationBean<SemesterSummary>(page, totalRecords);
      List<SemesterSummary> resultList =
          semesterSummaryMapper.querySemesterSummaryPageManagerList(semesterSummary,
              new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                  pageHolder.getPageSize()));
      pageHolder.setPageList(resultList);
      return pageHolder;
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(
          "querySemesterSummaryPageManagerList [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(), ToStringBuilder.reflectionToString(
              semesterSummary + "===" + page, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.ISemesterSummaryService#exportSemesterSummaryList(cn.qidisoft.edu.hzjt.model.SemesterSummary)
   */
  @Override
  public List<SemesterSummary> exportSemesterSummaryList(SemesterSummary semesterSummary) {
    try {
      return semesterSummaryMapper.exportSemesterSummaryList(semesterSummary);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("exportSemesterSummaryList [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(semesterSummary, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }



}
